﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmEditProduse : Form
    {
        private string sCif;
        DataSet dsFurnizori = new DataSet();
        DataTable dtFurnizori = new DataTable();
        SqlDataAdapter daFurnizori = new SqlDataAdapter();
        string sFurn = string.Empty;
        SqlCommand commFurn = new SqlCommand();
        public static SqlConnection connStatic;
        SqlConnection conn;
        private static Form _singletonInstance = null;
        private Form formCalling;
     
        public frmEditProduse(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;

        }

        public void SetKeyValues(string sCif, Form formCalling)
        {
            this.sCif = sCif;
            this.formCalling = formCalling;
        }

        private void btnIesire_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static Form GetSingletonInstance(SqlConnection conection)
        {
            if (_singletonInstance == null || _singletonInstance.IsDisposed)
            {
                connStatic = conection;
                _singletonInstance = new frmEditProduse(connStatic);
            }
            return _singletonInstance;
        }

        private void frmEditProduse_Load(object sender, EventArgs e)
        {
            sFurn = "SELECT * FROM furnizori WHERE furn_cif = '" + sCif + "'";

            daFurnizori = new SqlDataAdapter(sFurn, conn);
            dsFurnizori = new DataSet();

            daFurnizori.Fill(dsFurnizori, "furnizori");

            dtFurnizori = dsFurnizori.Tables[0];

            txtDenumire.Text = dtFurnizori.Rows[0]["furn_denumire"].ToString();
            txtCif.Text = sCif;
            txtCod.Text = dtFurnizori.Rows[0]["furn_cod"].ToString();
            txtAdresa.Text = dtFurnizori.Rows[0]["furn_adresa"].ToString();
            txtBanca.Text = dtFurnizori.Rows[0]["furn_banca"].ToString();
            txtIban.Text = dtFurnizori.Rows[0]["furn_iban"].ToString();
            txtEmail.Text = dtFurnizori.Rows[0]["furn_email"].ToString();
            txtRegCom.Text = dtFurnizori.Rows[0]["furn_regc"].ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati salvarea furnizorului cu noile date?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
            try
            {
                CustomProcedures.conn = connStatic;
                CustomProcedures.UPDATEFURNIZORI(txtRegCom.Text,
                                                 txtDenumire.Text,
                                                 txtAdresa.Text,
                                                 txtCod.Text,
                                                 txtIban.Text,
                                                 txtBanca.Text,
                                                 txtEmail.Text,
                                                 txtCif.Text
                                                 );
                MessageBox.Show("Salvarea furnizorului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                ((frmFurnizori)formCalling).CallBack();
                this.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Datele despre furnizor nu au fost salvate. S-au inregistrat erori!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnSterge_Click(object sender, EventArgs e)
        {
            switch (MessageBox.Show("Doriti sa efectuati stergerea furnizorului?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            { 
                case DialogResult.Yes:
                    break;
                case DialogResult.No:
                    return;
                    break;
            }
           
            try
            {
                sFurn = "DELETE FROM furnizori WHERE furn_cif = '" + sCif +"'";

                commFurn = new SqlCommand(sFurn, conn);


                SqlDataReader readerFurn = commFurn.ExecuteReader();

                readerFurn.Dispose();
                readerFurn.Close();
                txtDenumire.Text = txtCod.Text = txtCif.Text = txtRegCom.Text = txtBanca.Text = txtEmail.Text = txtAdresa.Text = txtIban.Text = string.Empty;

                MessageBox.Show("Stergerea furnizorului s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ((frmFurnizori)formCalling).CallBack();
                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Stergerea furnizorului nu s-a efectuat. S-au intampinat erori la stergere!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }
}
